var letteroffset = 0;
var yoffset = 0;
$(document).ready(function () {adjustSizes(); drawinterface();});
$(window).resize(function(){adjustSizes();});
function adjustSizes()
{
		var hcontent=$(window).height()-20;
		var wcontent=$(window).width()-20;
		
		$('#canvas').height(hcontent);
		$('#canvas').width(wcontent);
}
function drawinterface()
{
	//$.post('./interface.txt', function(responseText){ preparedata(responseText);});
	
	prepareletters("bureaucracy is a parasite that preys on free thought and suffocates free spirit...  douglas adams");
}
function preparedata(data)
{	
	var polygons = data.split(",");
	var Boxobj = { Xmin: 0, Xmax: $(window).width()-20, Ymin: 0, Ymax: $(window).height()-20};
	draw(polygons, Boxobj, true, "330066", 0);
}
function prepareletters(word)
{
	
		for (var i = 0; i < word.length; i ++)
		{
			if(word[i] == "."){file = "cardana/Void/dot.txt";}
			else if(word[i] == " "){letteroffset = letteroffset+ 40; continue;}
			else{file = "cardana/Void/"+ word[i] + ".txt";}
			jQuery.ajax({url: file, success: function(responseText){ renderletters(responseText)},async: false});
		}
}
function renderletters(letter)
{
	letter = letter.replace(/[\r\t]/g,"");
	var polygons = letter.split("POLYLINE");
	var stream = new Array();
	var m = 1;var p =0;
	stream[0] = "@";
	var alllinesx = new Array();
	var alllinesy = new Array();
	
	for (var i = 1; i < polygons.length-1; i++)
	{
		var sub = polygons[i].split("\n");
		
		for (var j = 1; j < sub.length-1; j++)
		{
			var subsub = sub[j].split(",");
			stream[m] = subsub[0];
			stream[m+1] = subsub[1];
			m = m+2;
			
			alllinesx[p] = parseFloat(subsub[0]);
			alllinesy[p] = parseFloat(subsub[1]);
			p++;
		}
		stream[m] = "@";
		m++;
		
	}
var BBox = {Xmin: Math.min.apply(null, alllinesx), Xmax: Math.max.apply(null, alllinesx), Ymin: Math.min.apply(null, alllinesy), Ymax: Math.max.apply(null, alllinesy)};
var Boxobj = { Xmin: 0, Xmax: $(window).width()-20, Ymin: 0, Ymax: $(window).height()-20};

var letteroff = BBox.Xmax-BBox.Xmin;

var color = Math.floor(Math.random()*16777215).toString(16);

draw(stream, Boxobj, true, "FFFFFF", letteroffset);

letteroffset = letteroffset +letteroff
}
function draw(data, Boxobj, fill, fillcolor, offset)
{	
	var canvas = document.getElementById("canvas");
	
	var newdata = transform(data, Boxobj, 400,150, offset);
		
	if (canvas.getContext)
	{
		var lines = canvas.getContext("2d");
		lines.beginPath();
		
		for (var i=0; i < newdata.length-1; i=i)
		{
			if(newdata[i] == "@"){
			lines.moveTo(newdata[i+1],newdata[i+2]); i++;}
			else
			{
			lines.lineTo(newdata[i],newdata[i+1]);
			i=i+2;
			}

        	}
        	
        	if(fill == true)
        	{
        	lines.fillStyle   = '#'+fillcolor;
        	lines.fill();
        	}
        	lines.strokeStyle = "#FFFFFF";
        	lines.stroke();
        	lines.closePath();
	}
}
function transform(data, Boxobj, width , height, offset)
{
	var newdata = new Array();
	
	lw = Boxobj.Xmax-Boxobj.Xmin;
	if(parseFloat(((offset-Boxobj.Xmin)/lw)*width) > 1350)
		{
			yoffset = yoffset+150;
			letteroffset =0;
			offset =0;
		}
	
	for (var i =0; i < data.length; i=i)
	{
		if (data[i] == "@"){newdata[i] ="@"; i++;}
		else
		{
			if(offset != 0){newdata[i] = parseFloat(((data[i]-Boxobj.Xmin)/lw)*width)+ parseFloat(((offset-Boxobj.Xmin)/lw)*width);}
			else{newdata[i] = parseFloat(((data[i]-Boxobj.Xmin)/lw)*width);}
			
			newdata[i+1] = parseFloat(height-((data[i+1]-Boxobj.Ymin)/lw)*width)+yoffset;
			i=i+2;
		}
	}
	return newdata;
}
